package game.utils.touch 
{
	import flash.geom.Point;
	import starling.display.DisplayObject;
	import starling.events.EventDispatcher;
	import starling.events.Touch;
	import starling.events.TouchEvent;
	import starling.events.TouchPhase;
	/**
	 * ...
	 * @author Thot
	 */
	public class TouchManager extends EventDispatcher
	{
		
		private var _content:DisplayObject;
		
		public function TouchManager() 
		{
			_content = null;
		}
		
		public function get content():DisplayObject 
		{
			return _content;
		}
		
		public function set content(value:DisplayObject):void 
		{
			if ( _content != null )
				stopListening();
				
			_content = value;
		}
		
		public function startListening():void
		{
			if ( _content == null )
				return;
				
			setEventListeners();
		}
		
		public function stopListening():void
		{
			if ( _content == null )
				return;
				
			_content.removeEventListeners();
		}
		
		
		
		private function setEventListeners():void
		{
			stopListening();
			_content.addEventListener( TouchEvent.TOUCH, touchHandler );
		}
		
        private function touchHandler(event:TouchEvent):void
        {
			var parent:DisplayObject = _content.parent || _content;
            var touches:Vector.<Touch> = event.getTouches(_content, TouchPhase.MOVED);
            
            if (touches.length == 1)
            {
                // one finger touching -> move
                var delta:Point = touches[0].getMovement(_content);
                _content.x += delta.x;
                _content.y += delta.y;
            }            
            else if (touches.length == 2)
            {
                // two fingers touching -> rotate and scale
                var touchA:Touch = touches[0];
                var touchB:Touch = touches[1];
                
                var currentPosA:Point  = touchA.getLocation(parent);
                var previousPosA:Point = touchA.getPreviousLocation(parent);
                var currentPosB:Point  = touchB.getLocation(parent);
                var previousPosB:Point = touchB.getPreviousLocation(parent);
                
                var currentVector:Point  = currentPosA.subtract(currentPosB);
                var previousVector:Point = previousPosA.subtract(previousPosB);
                
                var currentAngle:Number  = Math.atan2(currentVector.y, currentVector.x);
                var previousAngle:Number = Math.atan2(previousVector.y, previousVector.x);
                var deltaAngle:Number = currentAngle - previousAngle;
                
				// update pivot point based on previous center
				var previousLocalA:Point  = touchA.getPreviousLocation(_content);
				var previousLocalB:Point  = touchB.getPreviousLocation(_content);
				_content.pivotX = (previousLocalA.x + previousLocalB.x) * 0.5;
				_content.pivotY = (previousLocalA.y + previousLocalB.y) * 0.5;
				
				// update location based on the current center
				_content.x = (currentPosA.x + currentPosB.x) * 0.5;
				_content.y = (currentPosA.y + currentPosB.y) * 0.5;
				
				// rotate
                _content.rotation += deltaAngle;

                // scale
                var sizeDiff:Number = currentVector.length / previousVector.length;
                _content.scaleX *= sizeDiff;
                _content.scaleY *= sizeDiff;
            }
            
            var touch:Touch = event.getTouch(_content, TouchPhase.ENDED);
            
            if (touch && touch.tapCount == 2)
			{
				_content.alpha = ( _content.alpha == 0.5 ) ? 1 : 0.5;
				// on double tap
			}
			
			touch = event.getTouch(_content, TouchPhase.HOVER);       
			
			if ( touch )
			{
				// on hover
			}
			
        }
		
		
		
		public function destroy():void
		{
			stopListening();
			_content = null;
		}
		
	}

}